/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/5/2
 */
import type { RouteRecordRaw } from 'vue-router'
import { createRouter, createWebHashHistory } from 'vue-router'
import { useSystem } from '@/store'
import { defaultRoute, DEFAULT_ROUTE_PREFIX } from './static'

export { DEFAULT_ROUTE_PREFIX } from './static'

// 后端布局入口
export const layout: RouteRecordRaw = {
    path: `/${DEFAULT_ROUTE_PREFIX}`,
    name: DEFAULT_ROUTE_PREFIX,
    meta: { breadcrumb: false },
    component: () => import('@/pages/layout/index.vue'),
    children: [],
}

const router = createRouter({
    history: createWebHashHistory('admin'),
    routes: [...defaultRoute, layout],
})

router.beforeEach(async (to) => {
    const system = useSystem()
    const auth = to.meta.auth ?? true
    if (auth !== false && !system.tokenValid()) {
        return { name: 'login' }
    } else {
        return true
    }
})

export default router
